package com.wst.business.entity;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.TableName;
import com.wst.system.config.cache.SysCacheServer;
import com.wst.system.server.entity.DataBase;
import com.wst.system.server.entity.SysOpenUser;
import com.wst.system.server.service.SysOpenUserService;
import com.wst.system.util.SpringContextUtils;
import com.wst.system.util.TimeUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 
 * </p>
 *
 * @author X.I.O
 * @since 2025-03-21
 */

@Data
@ApiModel(description = "")
@TableName("sub_title")
public class SubTitle extends DataBase implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value="id", type= IdType.AUTO)
    @ApiModelProperty(value = "")
    private Integer id;

    @ApiModelProperty(value = "")
    private String title;
    /**
    * 内容
    * */
    @ApiModelProperty(value = "内容")
    private String contentText;
    /**
    * 图片列表
    * */
    @ApiModelProperty(value = "图片列表")
    private String imgList;
    /**
    * 用户id
    * */
    @ApiModelProperty(value = "用户id")
    private String uid;


    private String extData;

    @TableField(exist = false)
    public SubTitleExtData subTitleExtData;

    @TableField(exist = false)
    private String timeAgoStr;

    @TableField(exist = false)
    private String userHeadImgUrl;

    @TableField(exist = false)
    private String userNickName;

    @TableField(exist = false)
    private String vipLevel;

    public SysOpenUser getSysOpenUser() {
        if(StringUtils.isNotBlank(this.uid)) {
            SysCacheServer sysCacheServer=SpringContextUtils.getBean(SysCacheServer.class);
            SysOpenUserService sysOpenUser=SpringContextUtils.getBean(SysOpenUserService.class);
            sysCacheServer.get("uid_"+this.uid,(e)->{
                return sysOpenUser.getById(this.uid);
            });
        }
        return null;
    }

    public String getVipLevel() {
        SysOpenUser sysOpenUser=getSysOpenUser();
        if(sysOpenUser!=null && sysOpenUser.getViplevel()!=null) {
            return sysOpenUser.getViplevel();
        }
        return "1";
    }

    public String getUserHeadImgUrl() {
        SysOpenUser sysOpenUser=getSysOpenUser();
        if(sysOpenUser!=null && sysOpenUser.getHeadImg()!=null) {
            return sysOpenUser.getHeadImg();
        }
        return "/sys-file/res/donload?pk=TNCUf5Za3RNwwKEcKXXAkQ%3D%3D&es=1tUj3oo6BnssOiQBpepqI4qtG4eV0T30W38qv6cBBzk6pHr%2FJQsjt8nI7W1YLEZR";
    }

    public String getUserNickName() {
        SysOpenUser sysOpenUser=getSysOpenUser();
        if(sysOpenUser!=null && sysOpenUser.getNickname()!=null) {
            return sysOpenUser.getNickname();
        }
        return "天天短文默认用户8312";
    }

    public SubTitleExtData getSubTitleExtData() {
        if(StringUtils.isNotBlank(extData)){
            return JSONObject.parseObject(extData, SubTitleExtData.class);
        }
        return null;
    }

    public String getTimeAgoStr() {
        if(super.getCreateTime() != null){
            return TimeUtils.getRelativeTime(super.getCreateTime());
        }
        return null;
    }

    

}